| |

VerySource

 Forgot password?
 Register
Search
View: 836|Reply: 6

Datagrid problems, experts invited

[Copy link]

3

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-1-21 23:00:01
| Show all posts |Read mode
Have a datagrid,
One of the columns:
<asp: TemplateColumn HeaderText = "Business Class">
      <HeaderStyle Width = "40px"> </ HeaderStyle>
     <ItemTemplate>
<% # DataBinder.Eval (Container.DataItem, "ywdesp")%>
     </ ItemTemplate>
      <EditItemTemplate>
<asp: dropdownlist ID = "yk" runat = "server">
<asp: listitem Value = "0" Text = "Other" />
<asp: listitem Value = "1" Text = "内 一" />
<asp: listitem Value = "2" Text = "内 二" />
<asp: listitem Value = "3" Text = "Export" />
<asp: listitem Value = "4" Text = "marketing" />
</ asp: dropdownlist>
     </ EditItemTemplate>
</ asp: TemplateColumn>
In the non-editing state, "Other", "Inner one", "Inner two", "Export", "Marketing" and the like are displayed, but I want to yk (DropDownList) when in the "Edit" state SelectedIndex = 0, "Internal one" SelectedIndex = 1, ...

But I use the following code, the operation is not successful, please help, thank you!

public void gridUserItemDataBound (object sender, DataGridItemEventArgs e)
    {
if (e.Item.ItemType! = ListItemType.Header)
{
        switch (e.Item.Cells [3] .Text)
{
case "Other":
deptIndex = 0;
break;
case "内 一":
deptIndex = 1;
break;
case "内 二":
deptIndex = 2;
break;
case "Export":
deptIndex = 3;
break;
case "Marketing":
deptIndex = 4;
break;
}
Ranch
if (e.Item.ItemType == ListItemType.EditItem)
{
DropDownList myddl = (DropDownList) e.Item.FindControl ("yk");
myddl.SelectedIndex = deptIndex;
}
}
    }
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-31 14:36:01
| Show all posts
public void gridUserItemDataBound (object sender, DataGridItemEventArgs e)
    {int deptIndex;
if (e.Item.ItemType! = ListItemType.Header)
{
 switch (dt.rows [e.item.itemindex]. [your field name "])
{
case "Other":
deptIndex = 0;
break;
case "内 一":
deptIndex = 1;
break;
case "内 二":
deptIndex = 2;
break;
case "Export":
deptIndex = 3;
break;
case "Marketing":
deptIndex = 4;
break;
}

if (e.Item.ItemType == ListItemType.EditItem)
{
DropDownList myddl = (DropDownList) e.Item.FindControl ("yk");
myddl.SelectedIndex = deptIndex;
}

}

}
Reply

Use magic Report

3

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-1-31 18:45:01
| Show all posts
switch (dt.rows [e.item.itemindex]. [your field name "])

Where does this dt come from and what is it?
Reply

Use magic Report

0

Threads

58

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-1-31 21:09:01
| Show all posts
// int deptIndex;

>>>>> try:

public int deptIndex
{
    get {return (int) ViewState ["deptIndex"];}
    set {ViewState ["deptIndex"] = value;}
}
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-31 21:27:01
| Show all posts
Are you sure it is e.Item.ItemType! = ListItemType.Header, I think it should be ListItemType.DataItem,
Header is the header line, and your code is not executed all at once. So there will be problems.
Reply

Use magic Report

0

Threads

43

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-1-31 22:00:02
| Show all posts
e.Item.Cells [3] .Text is no longer available in edit mode
I also encountered this problem a few days ago. I dealt with it like this, please refer to it.

The tooltip of dropdownlist is bound to the value before editing:
<asp: dropdownlist ID = "yk" runat = "server" ToolTip = '<% # DataBinder.Eval (Container.DataItem, "ywdesp")%>'>
<asp: listitem Value = "0" Text = "Other" />
<asp: listitem Value = "1" Text = "内 一" />
<asp: listitem Value = "2" Text = "内 二" />
<asp: listitem Value = "3" Text = "Export" />
<asp: listitem Value = "4" Text = "marketing" />
</ asp: dropdownlist>

Background: switch (yk.ToolTip)
Reply

Use magic Report

3

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-2-1 10:45:01
| Show all posts
toyfacmilan

How clever you are!

Thank you!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list