1

ASP.netWebフォームにかなりの数のradiobuttonListがあります。以下に示す方法を使用して、それらを動的にバインドしています。

public static void PopulateRadioButtonList(DataTable currentDt, RadioButtonList currentRadioButtonList, string strTxtField, string txtValueField,
            string txtDisplay)
        {
            currentRadioButtonList.Items.Clear();
            ListItem item = new ListItem();
            currentRadioButtonList.Items.Add(item);
            if (currentDt.Rows.Count > 0)
            {
                currentRadioButtonList.DataSource = currentDt;
                currentRadioButtonList.DataTextField = strTxtField;
                currentRadioButtonList.DataValueField = txtValueField;
                currentRadioButtonList.DataBind();
            }
            else
            {
                currentRadioButtonList.Items.Clear();
            }
        }

ここで、RadioButtonアイテムテキストのDataTextFieldの最初の文字のみを表示したいと思います。

たとえば、値が良好の場合はGを表示したいだけです。公正の場合はFを表示したいのです。

C#でこれを行うにはどうすればよいですか

ありがとう

4

2 に答える 2

3

バインディングを行うと、やりたいことができないため、次の2つのオプションがあります。

  1. バインディングを実行する前に、テーブルから取得したデータを変更します。

  2. バインド後、各アイテムを確認し、そのテキストフィールドを変更します。

したがって、「RadioButtonアイテムテキストのDataTextFieldの最初の文字のみ」を表示する場合は、次の操作を実行できます。

currentRadioButtonList.DataSource = currentDt;
currentRadioButtonList.DataTextField = strTxtField;
currentRadioButtonList.DataValueField = txtValueField;
currentRadioButtonList.DataBind();

foreach (ListItem item in currentRadioButtonList.Items) 
    item.Text = item.Text.Substring(0, 1);

誤解していて、[値]フィールドの最初の文字を表示したい場合は、最後の2行を次のように置き換えることができます。

foreach (ListItem item in currentRadioButtonList.Items) 
    item.Text = item.Value.Substring(0, 1);
于 2010-10-12T18:14:19.580 に答える
0

バインドされているタイプ(Good、Fairなどを含むタイプ)にプロパティを追加して、このプロパティにバインドすることができます。常に最初の文字を使用する場合は、そのようにすることができます(もちろん、nullチェックを追加します)。

    public string MyVar { get; set; }

    public string MyVarFirstChar
    {
        get { return MyVar.Substring(0, 2); }
    }
于 2010-10-12T18:26:01.330 に答える